package ui.robots_wicket;
import org.apache.wicket.Page;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.CompoundPropertyModel;

import unq.uis.robots.applicationsModels.JugadorVista;
import unq.uis.robots.applicationsModels.VenderRobotApplicationModel;
import unq.uis.robots.model.Robot;


public class VenderPage extends WebPage{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JugadorVista jugadorVista;
	private CompoundPropertyModel<VenderRobotApplicationModel> venderCompoundPropertyModel;
	private VenderRobotApplicationModel venderApplicationModel;
	private final Page paginaAnterior;
	
	public VenderPage(Page page, Robot robotAVender){
		this.paginaAnterior = page;
		this.jugadorVista = (JugadorVista) page.getDefaultModelObject();
		this.venderApplicationModel = new VenderRobotApplicationModel(this.jugadorVista, robotAVender);
		this.venderCompoundPropertyModel = this.createModel(robotAVender);
		@SuppressWarnings({ "unchecked", "rawtypes" })
		Form form = new Form("venderForm", this.venderCompoundPropertyModel);						
		this.agregarLabels(form);
		this.agregarBotones(form);
		this.add(form);
	}
	
	private CompoundPropertyModel<VenderRobotApplicationModel> createModel(Robot robotAVender){
		return new CompoundPropertyModel<VenderRobotApplicationModel>(venderApplicationModel);
	}

	private void agregarLabels(Form<?> form) {
		form.add(new Label("nombre", this.venderCompoundPropertyModel.bind("robotAVender.nombre")));
		form.add(new Label("deterioro", this.venderCompoundPropertyModel.bind("robotAVender.danio")));
		form.add(new Label("precio", this.venderCompoundPropertyModel.bind("robotAVender.precio")));
		form.add(new Label("ofertaDelSistema", this.venderCompoundPropertyModel.bind("ofertaDelSistema")));
	}

	private void agregarBotones(Form<?> form) {
		form.add(new Button("aceptar") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				VenderPage.this.venderApplicationModel.vender();
				volver();
			}
		});
		
		form.add(new Button("cancelar"){
			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				VenderPage.this.venderApplicationModel.rechazarOferta();
				volver();
			}
		});
	}

	protected void volver() {
		this.setResponsePage(paginaAnterior);
	}
	
}
